package com.oplink.webbase.modules.cfg.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.oplink.webbase.common.persistence.Page;
import com.oplink.webbase.modules.cfg.entity.CfgSite;
import com.oplink.webbase.modules.cfg.services.CfgSiteService;
import com.oplink.webbase.modules.sys.dto.MsgDTO;
import com.oplink.webbase.modules.sys.dto.RespDTO;

/**
 * Title: 站点配置控制器
 * Description:
 * Copyright: Copyright (c) 2014
 * Company: Oplink Communications
 * 
 * @author
 * @serialData 2014-9-17 上午11:33:18
 * @version 1.0
 */
@Controller
@RequestMapping(value = "cfg/site")
public class CfgSiteController {

	@Autowired
	private CfgSiteService siteService;

	@RequestMapping(value = "")
	public String neCfg(CfgSite entity, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<CfgSite> page = siteService.find(new Page<CfgSite>(request, response), entity);
		model.addAttribute("pages", page);
		model.addAttribute("entity", entity);
		return "modules/cfg/siteIndex";
	}

	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	@ResponseBody
	public CfgSite updateForm(@PathVariable("id") Long id) {
		CfgSite entity = siteService.getEntity(id);
		return entity;
	}

	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public RespDTO update(@Valid @ModelAttribute("entity") CfgSite entity, RedirectAttributes redirectAttributes) {
		RespDTO res = new RespDTO(RespDTO.ERROR, "");
		try {
			if ((entity != null) && siteService.existSiteName(entity.getSname())) {
				siteService.save(entity);
				redirectAttributes.addFlashAttribute("message", "更新任务成功");
				res.setResult(RespDTO.SUCCESS);
				res.setMessage(MsgDTO.OK_SAVE_SUCCESS);
			} else {
				res.setMessage(MsgDTO.ERR_EXIST_SITE_NAME);
			}
			return res;
		} catch (Exception e) {
			return res;
		}
	}

	@RequestMapping(value = "create", method = RequestMethod.POST)
	@ResponseBody
	public RespDTO create(@Valid @ModelAttribute("entity") CfgSite entity, RedirectAttributes redirectAttributes,
			Model model) {
		RespDTO res = new RespDTO(RespDTO.ERROR, "");

		try {
			if ((entity != null) && siteService.existSiteName(entity.getSname())) {
				model.addAttribute("entity", entity);
				Long id = siteService.save(entity);
				redirectAttributes.addFlashAttribute("message", "创建任务成功");
				res.setResult(RespDTO.SUCCESS);
				res.setMessage(id + "");
			} else {
				res.setMessage(MsgDTO.ERR_EXIST_SITENAME);
			}
			return res;
		} catch (Exception e) {
			e.printStackTrace();
			return res;
		}
	}

	@RequestMapping(value = "delete/{id}")
	@ResponseBody
	public RespDTO delete(@PathVariable("id") String id, RedirectAttributes redirectAttributes) {
		RespDTO ret = new RespDTO(RespDTO.ERROR, "");
		try {
			if (!"".equals(id)) {
				String[] delList = id.split(",");
				for (String idItem : delList) {
					CfgSite site = siteService.getEntity(Long.valueOf(idItem));
					if (site.getNelist().size() != 0) {
						ret.setMessage(MsgDTO.ERR_NODE_EXIST_SITE);
						return ret;
					}
					siteService.delete(Long.valueOf(idItem));
					CfgSite entity = new CfgSite();
					entity.setSid(Long.valueOf(idItem));
				}
				ret.setResult(RespDTO.SUCCESS);
			} else {
				ret.setMessage(MsgDTO.ERR_NOT_FOUND_SITE);
			}
		} catch (Exception e) {
			ret.setMessage(e.getMessage());
		}
		return ret;
	}

}
